#include <iostream>
#include <unordered_map>
#include <fstream>

std::string defaultpath = "./Dict.txt";
std::string sep = ": ";

class Dict
{
public:
    bool load()
    {
        std::ifstream in(_dict_conf_filepath);
        if (!in.is_open())
        {
            return false;
        }

        std::string line;
        while (std::getline(in, line))
        {
            if (line.empty())
                continue;
            auto pos = line.find(sep);
            if (pos == std::string::npos)
                continue;

            std::string word = line.substr(0, pos); // 左闭右开
            if (word.empty())
                continue;
            std::string han = line.substr(pos + sep.size());
            if (han.empty())
                continue;

            // 日志

            _dict.insert(std::make_pair(word, han));
        }

        in.close();
        // 日志
        return true;
    }

    Dict(std::string path = defaultpath) : _dict_conf_filepath(path)
    {
        load();
    }

    std::string Translate(std::string word)
    {
        auto iter = _dict.find(word);
        if (iter == _dict.end())
        {
            return "系统提示：没找到！！";
        }
        return iter->second;
    }

    ~Dict()
    {
    }

private:
    std::unordered_map<std::string, std::string> _dict;
    std::string _dict_conf_filepath;
};